package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Tag(name = "后台员工信息管理")
@RestController
@RequestMapping("/admin/system/user")
@Slf4j
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台员工列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        log.info("分页查询员工列表");
        Page<SystemUserItemVo> systemUserPage = Page.of(current, size);
        IPage<SystemUserItemVo> page = systemUserService.pageSystemUser(systemUserPage, queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "根据ID查询后台员工信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        log.info("根据ID查询员工信息：{}", id);
        SystemUserItemVo systemUserItemVo = systemUserService.selectById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台员工信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        log.info("保存或更新员工信息");
        if(systemUser.getPassword() != null){
            //如果传进来的密码不为空，对输入的密码进行md5加密
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        //保存并更新
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        log.info("判断用户名是否可用：{}", username);
        //就是根据用户名判断是否存在这个用户
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, username);
        SystemUser systemUser = systemUserService.getOne(queryWrapper);
        if (systemUser == null) {
            return Result.fail();
        }
        return Result.ok(true);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台员工信息")
    public Result removeById(@RequestParam Long id) {
        log.info("根据ID删除员工信息：{}", id);
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台员工状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        log.info("根据ID修改员工状态");
        SystemUser systemUser = systemUserService.getById(id);
        systemUser.setStatus(status);
        systemUser.setUpdateTime(new Date());
        systemUserService.updateById(systemUser);
        return Result.ok();
    }
}
